// pages/cardbark/Land/Land.js
let app = getApp();
let testAddr = app.globalData.testAddr;
let imgAddr = app.globalData.imgurl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    login: [
      { id: "0", logname: "担保金" },
      { id: "1", logname: "好评" },
      { id: "2", logname:"所有类型"}
    ],
    currentTabsIndex: 0,
    products:[],
    credit:"",
    qualityGrade:"",
    provinceID:"",
    cityID:"",
    typeId: "",
    productTypeID:"",
    pageIndex:1,
    isSelectIndustry:false,
    twoLevels:[],
    threeLevels:[],
    twoSelectId:"",
    productThirdID:"",
    province:"全国"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    let that=this;
    let typeId=option.id;
    that.setData({
      typeId:typeId,
      qualityGrade:1,
    });
    wx.setNavigationBarTitle({
      title: option.name
    })
    this.queryProducts();
  },
  jumpSelectProvince(){
    wx.navigateTo({
      url: '../../citys/citys',
    })
  },
  queryProducts(){
    let that=this;
    app.util.request({
      url: testAddr + "Product/getComProduct?typeId=" + that.data.typeId + "&qualityGrade=" + that.data.qualityGrade + "&credit=" + that.data.credit + "&provinceID=" + that.data.provinceID + "&cityID=" + that.data.cityID + "&productTypeID=" + that.data.productTypeID + "&pageIndex=" + that.data.pageIndex + "&productThirdID" + that.data.productThirdID,
      method: "POST",
      success: function (res) {
        let products = res.data;
        for (let i in products) {
          products[i].img = imgAddr + "product/" + products[i].productimgs[0].imgurl;
        }
        that.setData({
          products: that.data.products.concat(products),

        });

      },
      fail: function (res) {

      },
      complete: function (res) {

      },
    });
  },
  onTabsItemTap: function (event) {
    var index = event.currentTarget.dataset.index;
    if(index===1){
      this.setData({
        currentTabsIndex:index,
        products: [],
        qualityGrade:"",
        credit:1,
      });
      this.queryProducts();
    }else if(index===0){
      this.setData({
        currentTabsIndex: index,
        products: [],
        qualityGrade: 1,
        credit: "",
      });
      this.queryProducts();
    }else{
      this.setData({
        isSelectIndustry: true
      });
      this.queryMenus();
    }
  },
  queryMenus(){
    let that = this;
    app.util.request({
      url: testAddr + "ProductType/getAllType",
      method: "POST",
      success: function (res) {
        let twoLevels = res.data;
        that.setData({
          twoLevels: twoLevels,
          twoSelectId: twoLevels[0].ID
        });
      },
      fail: function (res) {

      },
      complete: function (res) {

      },
    });

    app.util.request({
      url: testAddr + "ProductType/getProductById?typeid=" + that.data.typeId + "&productTypeId=" + that.data.twoSelectId,
      method: "POST",
      success: function (res) {
        let threeLevels = res.data;
        console.log(threeLevels);
        that.setData({
          threeLevels: threeLevels,
        });
      },
      fail: function (res) {

      },
      complete: function (res) {

      },
    });
  },
  hiddenSelectShow(){
    this.setData({
      isSelectIndustry: false
    });
  },
  selectFirstCol(event){
    let id = event.currentTarget.dataset.tid;
    if (id ==0) {
      this.setData({
        productTypeID:"",
        productThirdID:"",
        products:[],
        isSelectIndustry: false,
      });
      this.queryProducts();
      return;
    }
    console.log(id);
    this.setData({
      twoSelectId: id,
      productTypeID:id
    });
  },
  selectSecondCol(event){
    let id = event.currentTarget.dataset.tid;
    if (id == 0) {
      this.setData({
        productThirdID: "",
        products: [],
        isSelectIndustry: false,
      });
      this.queryProducts();
      return;
    }
    console.log(id);
    this.setData({
      productThirdID: id,
      products: [],
      isSelectIndustry: false,
    });
    this.queryProducts();
  },
  disabledShowSelect(){
    this.setData({
      isSelectIndustry: false,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    that.setData({
      pageIndex: that.data.pageIndex + 1
    });
    this.queryProducts();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})